﻿using System;
using System.Collections.Generic;
using JinianNet.Caching;
using System.Text;

namespace JinianNet.Web
{
    public partial class Cache
    {
        
        public class StaticCache : ICache
        {
            private static Dictionary<string, object> cache;
            static StaticCache()
            {
                cache = new Dictionary<string, object>();
            }

            #region ICache 成员

            public int Count
            {
                get { return cache.Count; }
            }

            public object this[string key]
            {
                get
                {
                    return cache[key];
                }
                set
                {
                    cache[key] = value;
                }
            }

            public object Get(string key)
            {
                object value;
                if (!cache.TryGetValue(key, out value))
                    value = null;
                return value;
            }

            public void Insert(string key, object value)
            {
                cache.Add(key, value);
            }

            public void Remove(string key)
            {
                cache.Remove(key);
            }

            public void RemoveAll()
            {
                Stack<string> remove = new Stack<string>();
                foreach (KeyValuePair<string, object> value in cache)
                {
                    remove.Push(value.Key);
                }
                while (remove.Count > 0)
                {
                    Remove(remove.Pop());
                }
            }

            #endregion
        }
    }
}
